﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using WebAPITest.Models;
using WebAPITest.Models.Database;
using WebAPITest.Service;

namespace WebAPITest.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AuthenticationController : ControllerBase
    {
        //定义数据库上下文
        private readonly WebEnterpriseIIContext _db;

        /// <summary>
        /// 构造方法 构造注入
        /// </summary>
        public AuthenticationController(WebEnterpriseIIContext db)
        {
            _db = db;
        }
        //注入jwt服务
        private readonly IJWTService _jwtService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="jwtService"></param>
        public AuthenticationController(IJWTService jwtService)
        {
            _jwtService = jwtService;
        }
        //注入Username服务
        private readonly IUserService _userService;
        public AuthenticationController(IUserService userService)
        {
            _userService = userService;
        }
       
        /// <summary>
        /// 创建token
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string CreateToken(string name)
        {
            return _jwtService.CreateToken(name, 0, "", "");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public string UserInfo()
        {
            //获取用户信息
            return Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Name).Value;
        }
        //编写接口完成注册功能，保证用户名唯一
        [HttpGet]
        public string Reg(RegRequest request)
        {
            //if (_db.Users.Any(x => x.UserName == request.UserName))
            //    return "用户存在";
            //使用服务
            if (_userService.TfUserName(request.UserName))
                return "用户存在";

            //var user = new User()
            //{
            //    UserName = request.UserName,
            //    Email = request.Email,
            //    NickName = request.NickName,
            //    Password = request.Password,
            //    CreateTime = DateTime.Now,
            //    Salt = 0,
            //    UserLevel = 1,
            //};
            ////上下文.表名.操作方法(需要插入的数据对象)
            //_db.Users.Add(user);
            ////保存更改
            //var row = _db.SaveChanges();
            var UserId= _userService.Reg(request.UserName, request.Email, request.NickName, request.Password);
            if (UserId > 0)
            {
                return "注册成功";
            }
            return "注册失败";
        }
        [HttpPost]
        public string Login(string username, string password)
        {
            //第一步 从数据库查询用户为username的用户 
            //var user = _db.Users.FirstOrDefault(x => x.UserName == username);

            //使用服务查询
            var user = _userService.GetNameByUserName(username);
            //如果不存在用户就不存在
            if (user == null)
                return "用户不存在";
            //如果存在 判断查询出来的用户密码是否 跟password相等
            if (user.Password != password)
            {
                //如果不相等 返回密码不正确
                return "密码不正确";
            }
            //如果相等呢？？？
            //创建Token 返回token给前端
            return _jwtService.CreateToken(user.UserName, user.UserId, user.Email, user.NickName);
        }

        [ResponseCache(Duration =20)]
        [HttpGet]
        public DateTime Now()
        {
            return DateTime.Now;
        }
    }
}
